<?php
namespace app\user\model;

use app\common\model\User as UserModel;
use think\facade\Session;
use think\facade\Cache;

/**
 * 用户模型
 */
class User extends UserModel
{
    /**
     * 手机绑定
     */
    public function phoneBind($data)
    {
        if(Cache::get($data['phone'] . '_' . $data['captcha'])){
            Cache::delete($data['phone'] . '_' . $data['captcha']);
        }else{
            $this->error = '验证码错误';
            return false; 
        }
        if($user = User::where('phone',$data['phone'])->find()){
            if($user['user_id'] != $this->user_id){
                $this->error = '该手机号已被占用';
                return false;
            }
        }
        return $this->save($data) != false;
	}
	
	/**
     * 微信绑定
     */
    public function wechatBind($data)
    {
        if($user = User::where('open_id',$data['open_id'])->where('status','>',10)->find()){
            if($user['user_id'] != $this->user_id){
                $this->error = '该微信已绑定在其它账号';
                return false;
            }
        }
        return $this->save($data) != false;
	}
	
	/**
     * 代理登陆
     */
    public function agentLogin()
    {
    	if($this->status['value'] != 30){
    		$this->error = '您还不是代理';
            return false;
    	}
        // 保存登录状态
		Session::set('hema_agent', [
			'user' => $this,
			'is_login' => true
		]);
		return true;
    }
}
